import trim from 'lodash/trim';

export const convertSpecialChar = (str) =>{
    str = str.toString();
    str = str.replace(/<br>/ig, "\n");
    str = str.replace(/&nbsp;/ig, " ");
    str = str.replace(/&lt;/ig, "<");
    str = str.replace(/&gt;/ig, ">");
    str = str.replace(/&quot;/ig, "\"");
    str = str.replace(/&amp;/ig, "&");
    return str;
}

//签字意见验证
export const chekcremark = (remarkcontent,forwardremarkInfo) => {
    let hasvalue = true;
    const {signworkflowids="", signdocids="", handWrittenSign=0}=forwardremarkInfo;
    if(signworkflowids.length > 0 || signdocids.length > 0 || forwardremarkInfo["field-annexupload"].length > 0 || handWrittenSign > 0) {

    }else if(remarkcontent == null) {
        hasvalue = false;
    } else {
        let _remarkcontent = remarkcontent;
        _remarkcontent = _remarkcontent.replace(/<p>/ig,'').replace(/<\/p>/ig,'').replace(/ /ig, "").replace(/&nbsp;/ig, "").replace(/<br \/>/ig,"");
        _remarkcontent = _remarkcontent.replace(/\\n/ig, "");
        if(trim(_remarkcontent).length  === 0) {
            hasvalue = false;
        }
    }
    return hasvalue;
}

export const realLength =(str)=>{
    let j = 0;
    for(var i = 0; i <= str.length - 1; i++) {
        j = j + 1;
        if((str.charCodeAt(i)) > 127) {
            j = j + 2;
        }
    }
    return j;
}

/**
 * 设置意见默认样式
 * @param _remark
 * @param wfSignDefaultFontFamily
 * @param wfSignDefaultFontSize
 * @return {*}
 */
export const appendDefaultSytle = (_remark,wfSignDefaultFontFamily,wfSignDefaultFontSize,pcUseTextareaSigninput = 0) =>{
    if(_remark) {
        const preReg = new RegExp(
            /^<div id=\"wea_rich_text_default_font\"[^>]+\>/
        );
        if(preReg.test(_remark)) return _remark;
        //设置
        let defaultStyle = '';
        if(wfSignDefaultFontFamily && wfSignDefaultFontFamily.indexOf('/') > -1) {
            defaultStyle += `font-family:${wfSignDefaultFontFamily.split('/')[1]};`;
        }
        if(wfSignDefaultFontSize && wfSignDefaultFontSize.indexOf('/') > -1) {
            defaultStyle += `font-size:${wfSignDefaultFontSize.split('/')[1]};`;
        }
        if(defaultStyle) {
            if(pcUseTextareaSigninput == 1) {
                _remark = `<span id="wea_rich_text_default_font" style="${defaultStyle}">${_remark}</span>`;
            } else {
                _remark = `<div id="wea_rich_text_default_font" style="${defaultStyle}">${_remark}</div>`;
            }
        }
    }
    return _remark;
}

//位置串转成对象
export const getLocationInfoData = (remarkLocation = '') => {
  let locationInfoData = {};
  if (remarkLocation) {
      const locationInfos = remarkLocation.split(',');

      if (locationInfos.length === 4) {
          locationInfoData['longitude'] = locationInfos[1];
          locationInfoData['latitude'] = locationInfos[2];
          locationInfoData['name'] = locationInfos[3];
      }
      if( locationInfos.length === 3) {
        //来自pc端的位置信息
        locationInfoData['longitude'] = locationInfos[0];
        locationInfoData['latitude'] = locationInfos[1];
        locationInfoData['name'] = locationInfos[2];
      }
  }
  return locationInfoData;
}